﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GN.Pay.Application.GatewayApp;
using GN.Pay.TradeChannel;
using GN.Pay.Utils;
using GN.Pay.TradeChannel.Gyb;

namespace GN.Pay.GatewayWeb.Controllers.Notify
{
    /// <summary>
    /// 贵阳银行
    /// </summary>
    public class GybController : APayNotifyController
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tradeNotifyAppService"></param>
        public GybController(ITradeNotifyAppService tradeNotifyAppService)
            : base(tradeNotifyAppService)
        {

        }

        protected override NotifyMessage GetPayNotifyMessage(HttpRequestBase request)
        {
            var requestDictionary = PreParam.GetQueryDictionary(request);
            var no = GybServiceBase.GetPayOrderNo(requestDictionary);
            return new NotifyMessage(no, requestDictionary);
        }

        protected override NotifyMessage GetRefundNotifyMessage(HttpRequestBase request)
        {
            var requestDictionary = PreParam.GetQueryDictionary(request);
            var no = GybServiceBase.GetRefundNo(requestDictionary);
            return new NotifyMessage(no, requestDictionary);
        }

        /// <summary>
        /// 
        /// </summary>
        protected override string ChannelId
        {
            get
            {
                return "Gyb.WebPay";
            }
        }

        public override void OrderPay()
        {
            DictionaryPayNotify();
        }

        private ActionResult OrderPayReturn(Func<ActionResult> errorActionResult, Func<string, ActionResult> actionResult)
        {
            try
            {
                var msg = GetPayNotifyMessage(Request);
                ITradeChannel channel = GetPayTradeChannel(msg);
                if (channel != null)
                {
                    try
                    {
                        if (!this.IsPayNotify(msg.UniqueNo))
                        {
                            channel.OrderPayNotify(this.tradeNotifyAppService, msg.Message);
                            this.SetPayNotify(msg.UniqueNo);
                        }
                        return actionResult(msg.UniqueNo);
                    }
                    catch (Exception err)
                    {
                        Logger.Error("贵阳银行付款通知错误", err);
                        return errorActionResult();
                    }
                }
                else
                {
                    Logger.Error("付款通知错误,无 " + ChannelId + " 实现");
                    return errorActionResult();
                }
            }
            catch (Exception err)
            {
                Logger.Error(err.Message, err);
                return errorActionResult();
            }
        }

        public override ActionResult OrderPayReturn()
        {
            return OrderPayReturn(ErrorView, ReturnWaitResult);
        }

        public override ActionResult OrderPayWapReturn()
        {
            return OrderPayReturn(WapErrorView, ReturnWapWaitResult);
        }

        public override void OrderRefund()
        {
            throw new NotImplementedException();
        }


    }
}